package com.example.front.serivce;

import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.common.exception.BusinessException;
import com.example.front.dto.GatewayCreateDto;
import com.example.front.dto.GatewayDto;
import com.example.front.dto.GatewaySearchDto;
import com.example.front.entity.Gateway;
import com.example.front.entity.Line;
import com.example.front.mapper.GatewayMapper;
import com.example.interceptor.UserContext;
import com.example.passport.entity.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class GatewayService {

    @Resource
    GatewayMapper gatewayMapper;
    @Resource
    BoxService boxService;

    /**
     * 列出网关
     */
    public ListDto<GatewayDto> listGateway(GatewaySearchDto condition, Pagination pagination) {

        long total = gatewayMapper.count(condition);
        if (total <= 0) {
            return new ListDto<>(new ArrayList<>(), pagination);
        }

        pagination.setTotal(total);

        List<Gateway> gatewayList = gatewayMapper.findAll(condition, pagination);

        List<GatewayDto> gatewayDtoList = gatewayList.stream().map(gateway -> {
            GatewayDto gatewayDto = new GatewayDto();
            BeanUtils.copyProperties(gateway, gatewayDto);
            return gatewayDto;
        }).collect(Collectors.toList());

        return new ListDto<>(gatewayDtoList, pagination);
    }

    /**
     * 创建网关
     */
    @Transactional
    public void create(@Valid GatewayCreateDto gatewayCreateDto) {
        User me = UserContext.getUser();

        if (gatewayMapper.findByName(me.getTenantId(), gatewayCreateDto.getName()) != null) {
            throw new BusinessException("名称已存在");
        }

        if (gatewayMapper.findByCode(gatewayCreateDto.getCode()) != null) {
            throw new BusinessException("code已存在");
        }

        Gateway gateway = new Gateway();
        gateway.setTenantId(me.getTenantId());
        BeanUtils.copyProperties(gatewayCreateDto, gateway);
        gatewayMapper.create(gateway);
    }

    /**
     * 跟新数据
     */
    public void update(@Valid GatewayDto gatewayDto) {
        User me = UserContext.getUser();

        if (gatewayMapper.findByName(me.getTenantId(), gatewayDto.getName()) != null && gatewayMapper.findByIdName(me.getTenantId(), gatewayDto.getId(), gatewayDto.getName()) == null) {
            throw new BusinessException("名称已存在");
        }

        if (gatewayMapper.findByCode(gatewayDto.getCode()) != null && gatewayMapper.findByIdCode(me.getTenantId(), gatewayDto.getId(), gatewayDto.getCode()) == null) {
            throw new BusinessException("code已存在");
        }
        Gateway gateway = new Gateway();
        gateway.setTenantId(me.getTenantId());
        BeanUtils.copyProperties(gatewayDto, gateway);
        if (gatewayMapper.update(gateway) != 1) {
            throw new BusinessException("操作失败");
        }
    }

    /**
     * 删除
     */
    @Transactional
    public void deleteById(long id,long tenantId) {
        if(boxService.findByGatewayId(tenantId,id)!=0){
            throw new BusinessException("该网关下存在有按钮盒，请将该网关下的按钮盒删除或修改再执行该操作");
        }

        if (gatewayMapper.deleteById(id, tenantId) != 1) {
            throw new BusinessException("操作失败");
        }
    }
    /**
     * 搜索网关
     */
    public Gateway findByCode(String code) {
        if (gatewayMapper.findByCode(code) == null) {
            throw new BusinessException("网关不存在");
        }
        return gatewayMapper.findByCode(code);
    }

    /**
     * 获取网关信息
     */
    public Map<Long, Gateway> findByIds(List<Long> gatewayIds){

        Map<Long, Gateway> gatewayMap = gatewayMapper.findGatewayByIds(gatewayIds).stream()
                .collect(Collectors.toMap(Gateway::getId, gateway -> gateway));

        return gatewayMap;
    }

}
